﻿using DeMeng.Common.Config;
using DeMeng.Models;
using DeMeng.Models.Models;
using DeMeng.Models.Models.Poster;
using DeMeng.Models.Swagger;
using DeMeng.Services.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DeMeng.Api.Areas.Manager.Controllers
{
    [ApiExplorerSettings(GroupName = Grouping.V2)]
    [Route("Manager/[controller]")]
    [ApiController]
    [Authorize(Roles = "Boss,Manager")]
    public class PosterController : ControllerBase
    {
        private readonly IPosterServices _posterServices;

        public PosterController(IPosterServices posterServices) {

            _posterServices = posterServices;
        }

        #region 增、删、改、查


        /// <summary>
        /// 查
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost(nameof(LoadForm))]
        public async Task<ResponseModel> LoadForm(GuidQueryModel query)
        {
            var model = await _posterServices.QueryById(query.Id);
            if (model != null) { 
                model.Image = string.IsNullOrWhiteSpace(model.Image) ? "" : WebConfig.WebSiteUrl + model.Image;
            }
            return ResponseModel.Success(model == null ? new Models.Models.Poster.PosterEntity() { ID = 0 } : model);
        }

        /// <summary>
        /// 添、改
        /// </summary>
        /// <param name="model">实体信息</param>
        /// <returns></returns>
        [HttpPost(nameof(Save))]
        public async Task<ResponseModel> Save(PosterEntity model)
        {
            if (await _posterServices.Save(model))
            {
                return ResponseModel.Success(model);
            }
            throw new ApiException("保存失败");

        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost(nameof(FindList))]
        public async Task<ResponseModel> FindList(PosterListQueryModel query)
        {
            return ResponseModel.Success(await _posterServices.FindList(query));
        }


        /// <summary>
        /// 删
        /// </summary>
        /// <param name="query">ID</param>
        /// <returns></returns>
        [HttpPost(nameof(Delete))]
        public async Task<ResponseModel> Delete(GuidQueryModel query)
        {
            return ResponseModel.Success(await _posterServices.DeleteById(query.Id));
        }

        #endregion
    }
}
